<?php

namespace App\Http\Middleware;

use Closure;
use Tymon\JWTAuth\Facades\JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
use Tymon\JWTAuth\Exceptions\TokenInvalidException;

class authJWT
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $token = $request->header ("token");
        if (!$token) {
            return response ()->json ([
                "code" => 302,
            ]);
        }else{
            $token = json_decode (base64_decode ($token),true);
            if (!$token || $token["expire_time"] < time ()){
                return response ()->json ([
                    "code" => 302,
                ]);
            }
        }
        $request->user = $token["user"];
        return $next($request);
    }
}
